package com.bingyuan.aipassproject

import android.view.Gravity
import android.widget.RadioButton
import android.widget.Toast
import com.bingyuan.aipassproject.di.component.DaggerUIComponent
import com.bingyuan.aipassproject.view.BaseActivity
import com.bingyuan.aipassproject.view.fragment.MarketFragment
import com.bingyuan.aipassproject.view.fragment.MineFragment
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import kotlin.system.exitProcess

class MainActivity : BaseActivity() {
    private lateinit var marketFragment: MarketFragment
    private lateinit var mineFragment: MineFragment
    private var lastButton: RadioButton? = null // 上一次点击的底部图标
    private val timer = Timer()
    private var isExit: Boolean = false

    override fun getLayoutResId(): Int {
        return R.layout.activity_main
    }

    override fun initializeInjector() {
        super.initializeInjector()
        val uiComponent = DaggerUIComponent.builder().applicationComponent(applicationComponent)
            .activityModule(activityModule).build()
        uiComponent.inject(this)
    }

    override fun initView() {
        super.initView()
        marketFragment = MarketFragment()
        mineFragment = MineFragment()
        switchFragment(marketFragment,"marketFragment")
    }

    override fun onClick() {
        main_market_btn.setOnClickListener {
            lastButton = main_market_btn
            switchFragment(marketFragment, "MarketFragment")
        }
//        main_transaction_btn.setOnClickListener {
//            lastButton = main_classify_btn
//            switchFragment(classifyFragment, "ClassifyFragment")
//        }
//        main_assets_btn.setOnClickListener {
//            lastButton = main_classify_btn
//            switchFragment(classifyFragment, "ClassifyFragment")
//        }
        main_mine_btn.setOnClickListener {
            lastButton = main_mine_btn
            switchFragment(mineFragment, "MineFragment")
        }
    }

    override fun onBackPressed() {
        //判断是否退出
        if (!isExit) {
            isExit = true
            val toast = Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.show()            //通过TimerTask延时2秒发送  如果在2秒之内点击 则不走本次判断 直接执行else
            val timerTask = object : TimerTask() {
                override fun run() {
                    isExit = false
                }
            }
            //启动任务
            timer.schedule(timerTask, 2000)
        } else {
            //退出
            finish()
            timer.cancel()
            exitProcess(0)
            super.onBackPressed()
        }
    }
}